const uuid = require('node-uuid'),
    verificationCodeDictionary = [
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    ],
    securityCodeDictionary = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
const securityCodeDictionaryLength = securityCodeDictionary.length,
    verificationCodeDictionaryLength = verificationCodeDictionary.length;
/**
 * 生成短位安全码
 *
 * @param number
 */
function securityCode(number = 6) {
    let need = [],
        i = 0;
    do {
        let index = Math.floor(Math.random() * securityCodeDictionaryLength);
        need.push(securityCodeDictionary[index]);
        i++;
    } while (i < number);
    return need.join("");
};

/**
 * 生成随机码
 *
 * @param number
 * @returns {string}
 */
function randomCode(number = 4) {
    let need = [],
        i = 0;
    do {
        let index = Math.floor(Math.random() * verificationCodeDictionaryLength);
        need.push(verificationCodeDictionary[index]);
        i++;
    } while (i < number);
    return need.join("");
};

/**
 * 过滤html标记
 *
 * @param str
 * @returns {*}
 */
function delHtmlTag(str) {
    return str.replace(/<[^>]+>/g, "");
};

/**
 * UUID 生成器 生成唯一id，标示某个记录
 *
 * @returns {string}
 */
function uuidCode() {
    return uuid.v1().split("-").join("");
};



/**
 * 扁平化化嵌套数组
 * @param  {[type]} arr [description]
 */
function flatten(arr) {
    return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []);
}

/**
 * 数组排重
 * @param  {[type]} arr [description]
 */
function unique(arr) {
    return arr.filter((v, i, a) => a.indexOf(v) === i);
}

/**
 * 随机整型
 * @param  {[type]} min [description]
 * @param  {[type]} max [description]
 * @return {[type]}     [description]
 */
function randomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

/** 
 * 返回max范围内随机整数
 * @param  {[type]} max [description]
 * @return {[type]}     [description]
 */
function randomMax(max) {
    return randomInt(0, max);
}


/**
 * 数组求和
 * @param  {Array}  arr [description]
 * @return {[type]}     [description]
 */
function sumNum(arr = []) {
    let total = arr.reduce((sum, value) => {
        return sum + value;
    }, 0);
    return total;
}

/**
 * 分页
 * @param {[type]} Model         [sequlize model]
 * @param {Number} page          [description]
 * @param {Number} limit         [description]
 * @param {Object} filter        [description]
 * @yield {[type]} [description]
 */
function* pagenation(Model, page = 1, limit = 20, filter = {}) {
    page = +page;
    limit = +limit;
    let offset = (page - 1) * limit;
    return yield Model.findAndCountAll({
        where: filter,
        offset,
        limit,
        order: [
            ['createdAt', 'desc']
        ],
        raw: true
    });
}


module.exports = {
    randomCode,
    randomNum: securityCode,
    delHtmlTag,
    uuidCode,
    flatten,
    unique,
    randomMax,
    sumNum,
    pagenation
};